<template>
  <div class="user-container">
    <!-- 用户基本信息面板 -->
    <div class="user-card">
      <!-- 用户头像、姓名 -->
      <van-cell>
        <!-- 使用 title 插槽来自定义标题 -->
        <template #icon>
          <img :src="$store.state.userPhoto" alt="" class="avatar" />
        </template>
        <template #title>
          <span class="username">{{ userList.name }}</span>
        </template>
        <template #label>
          <van-tag color="#fff" text-color="#007bff">申请认证</van-tag>
        </template>
      </van-cell>
      <!-- 动态、关注、粉丝 -->
      <div class="user-data">
        <div class="user-data-item">
          <span>{{ userList.art_count }}</span>
          <span>动态</span>
        </div>
        <div class="user-data-item">
          <span>{{ userList.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="user-data-item">
          <span>{{ userList.fans_count }}</span>
          <span>粉丝</span>
        </div>
      </div>
    </div>

    <!-- 操作面板 -->
    <van-cell-group class="action-card">
      <van-cell icon="edit" title="编辑资料" is-link @click="userEditFn" />
      <van-cell icon="chat-o" title="AIPoPo" is-link to="/chat" />
      <van-cell icon="warning-o" title="退出登录" is-link @click="quitFn" />
    </van-cell-group>
  </div>
</template>

<script>
import { userAPI } from "@/api";
import { removeToken } from "@/utils/token.js";
import { Dialog, Toast } from "vant";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      userList: [], //用户信息
    };
  },
  created() {},
  activated() {
    this.getUserProfile();
  },
  methods: {
    // (第二种)用mapMutations（）方法映射
    ...mapMutations(["SET_USERPGOTO"]),
    async getUserProfile() {
      const { data: res } = await userAPI();
      // console.log(res);
      this.userList = res.data;
      // console.log(this.userList);
      // （第一种）直接修改VUEX里的数据
      // this.$store.commit("SET_USERPGOTO", this.userList.photo);
      // （第二种）
      this.SET_USERPGOTO(this.userList.photo);
      // this.userList.name = this.$store.state.userName;
    },

    // 点击退出事件
    quitFn() {
      Dialog.confirm({
        title: "是否退出登录",
        message: "真走了？",
      })
        .then(() => {
          //用户点击“确认”选项-》内部resolve触发then
          // 业务/思路/方向
          removeToken(); //清空用户token
          Toast.success("退出成功");
          this.$router.replace("/login"); //跳转登录页
          // on confirm
        })
        .catch(() => {
          //用户点击“取消”选项-》内部reject触发catch
          // on cancel
        });
    },
    // 点击跳转用户资料编辑页面
    userEditFn() {
      this.$router.push("/useredit");
    },
  },
  //
  // mounted() {
  //   this.$bus.$emit("imgList", this.userList);
  // },
};
</script>

<style scoped lang="less">
.user-container {
  .user-card {
    background-color: #575b60;
    color: white;
    padding-top: 20px;
    .van-cell {
      background: #575b60;
      color: white;
      &::after {
        display: none;
      }
      .avatar {
        width: 60px;
        height: 60px;
        background-color: #fff;
        border-radius: 50%;
        margin-right: 10px;
      }
      .username {
        font-size: 14px;
        font-weight: bold;
      }
    }
  }
  .user-data {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    font-size: 14px;
    padding: 30px 0;
    .user-data-item {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 33.33%;
    }
  }
}
</style>